package com.dcbeida.activity.hospital;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.HospitalListAdapter;
import com.dcbeida.bean.HospitalBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 医院选择
 * Created by swt on 2016/12/26.
 */

public class HospitalActivity extends BaseActivity implements HospitalContract.View {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.et_query)
    EditText etQuery;

    @Bind(R.id.myList)
    ListView myList;
    private HospitalPresenter mPresent;
    private HospitalListAdapter adapter;
    @Bind(R.id.lin_empty)
    LinearLayout lin_empty;


    @Override
    protected int getLayoutResId() {
        return R.layout.hospital_listview;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParams();
        initData();
    }

    @OnClick(R.id.img_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }

    private void initParams() {
        mPresent = new HospitalPresenter(this, this);
        mPresent.getHospitalList();
    }

    private void initData() {
        etQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(etQuery.getText().toString().trim())) {
                    mPresent.getHospitalInfo(etQuery.getText().toString().trim());
                } else {
                    mPresent.getHospitalList();
                }
            }
        });
        adapter = new HospitalListAdapter(HospitalActivity.this);
        myList.setEmptyView(lin_empty);
        myList.setAdapter(adapter);
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                HospitalBean hospitalBean = (HospitalBean) adapter.getItem(position);
                bundle.putSerializable("hospitalBean", hospitalBean);
                intent.putExtras(bundle);
                HospitalActivity.this.setResult(RESULT_OK, intent);
                HospitalActivity.this.finish();
            }
        });

    }

    @Override
    public void showTelTip(String msg) {
        showTip(msg);
    }

    @Override
    public void showHospitalList(ArrayList<HospitalBean> list) {
        adapter.setmArrayList(list);
    }

    @Override
    public void setPresenter(HospitalContract.Present presenter) {

    }
}
